﻿<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ClassMonthInput.aspx.cs" Inherits="Moral_Teacher_ScoreInput_ClassMonthInput" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
    <link href="../../Common/css/main.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
        </asp:ScriptManager>
        <div class="mainWarp" style="width: 98%; margin: auto;padding-left:5px;">

                    <div style="margin-top: 5px;">
                        <h2><%= ReportTitle %></h2>
                        <br /><br />
                        <%= ReportSubTitle %>
                        <input type="button" class="commonBut" id="btnReset" value="重置" onclick="reflesh();" />&nbsp;
                        <input type="button" class="commonBut" id="btnSave" value="保存" onclick="saveList();" />
                        <input type="button" class="commonBut" id="btnExport" value="导出Excel" onclick="getExcel()" style="display: none;" />
                    </div>

            <br />
            <div id="divTable" style="width:600px;  height:300px; margin:left; overflow: auto;position: relative;">
                <asp:Table ID="tblHead" runat="server" CssClass="useList" style="padding-top:0px;margin-top:0px" CellPadding="0" CellSpacing="0">
                </asp:Table>
            </div>
            <div id="divInputRemark" class="layoutWarp" style="display: none; width: 380px; height: 270px;">
                <div class="title">
                    <span id="spanInputRemarkTitle"></span><a alt="关闭" href="javascript:hideRemark();">关闭</a>
                </div>
                <textarea id="txtRemark" style="width: 370px; height: 200px;" maxlength="50" onkeyup="return MaxLength(this)"
                            onkeypress="return (/^[a-zA-Z0-9\u4e00-\u9fa5]+$/g.test(String.fromCharCode(event.keyCode)))"
                           onblur="value=value.replace(/[^\u4e00-\u9fa5a-zA-Z0-9]/g ,'')" 
                           onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))"></textarea>
                <input type="hidden" id="hidClassID" />
                <div style="text-align: center;">
                    <input type="button" class="commonBut" value="保存" onclick="saveRemark();" />
                    <input type="button" class="commonBut" value="取消" onclick="hideRemark();" /></div>
            </div>
        </div>
    <div id="upInputPop" class="layoutWarp" style="display: none">
        <iframe src="" name="downloadf" id="downloadf" style="height: 80%;width: 95%;overflow-y:auto;overflow-x:none;" frameborder="0"></iframe>
    </div>
    </form>
</body>
<script type="text/javascript" src="../../Common/js/jquery.js"></script>
<script type="text/javascript" src="../../Common/js/common.js"></script>
<script type="text/javascript" src="../../Common/js/jsUtil.js"></script>
<script type="text/javascript" src="../../../Common/js/TableForExport.js"></script>
<script type="text/javascript" src="../../../Common/js/ScoreInputUtil.js"></script>
<script type="text/javascript">

 function MaxLength(obj)
        {
            var mlength=obj.getAttribute? parseInt(obj.getAttribute("maxlength")) : ""
            if (obj.getAttribute && obj.value.length>mlength)
            obj.value=obj.value.substring(0,mlength)
        }

    window.onload = function() {
        var btnReset = document.getElementById("btnReset");
        var btnSave = document.getElementById("btnSave");
        var btnExport = document.getElementById("btnExport");
        if (request("Type") == "Edit") {
            btnReset.style.display = "inline";
            btnSave.style.display = "inline";
            btnExport.style.display = "none";
        }
        else {
            btnReset.style.display = "none";
            btnSave.style.display = "none";
            btnExport.style.display = "inline";
        }
    }
    
    // 重置按钮
    function reflesh() {
        window.location.href = window.document.location.href;
    } 
    
    // 显示备注弹出框
    function displayRemark(obj) {
        var className = obj.getAttribute("ClassName");
        ShowPopForm("divInputRemark", "spanInputRemarkTitle", className + "的备注");
        document.getElementById("hidClassID").value = obj.getAttribute("ClassID");
        document.getElementById("txtRemark").value = obj.childNodes[1].title;
    }
    
    // 隐藏备注弹出框
    function hideRemark() {
        ClosePopForm("divInputRemark");
        document.getElementById("txtRemark").value = "";
    }
    
    // 暂存备注信息
    function saveRemark() {
        var txtRemark = document.getElementById("txtRemark");
        if (txtRemark.value == "") {
            showNoticMsg("txtRemark", "请填写备注");
            return;
        }
        var title = txtRemark.value;
        var html = "";
        if (txtRemark.value.length > 20) {
            html = txtRemark.value.substr(0, 20) + "...";
        }
        else {
            html = txtRemark.value;
        }
        var id = "span_" + document.getElementById("hidClassID").value;
        var spanRemark = document.getElementById(id);
        spanRemark.innerHTML = html;
        spanRemark.title = title;
        hideRemark();
    }
        
    // 保存本次结果
    function saveList() {
        
        var list = [];
        var trList = $("#tblHead tr");
        var stuJsonList = "";
        var jsonList = "";
        
        for (var i=0; i<trList.length; i++) {
            if ($(trList[i]).find("table").length == 0) continue;
            var inputList = $(trList[i]).find("input");
            var spanRemark = $(trList[i]).find("span")[0];
            var inputState = false;
            var stuList = [];
            var classID;
            for (var j=0; j<inputList.length; j++) {
                var str = inputList[j].id;
                var val = inputList[j].value;
                if (val != "" && isNaN(val)) {
                    showNoticMsg(str, "请填写数字");
                    return false;
                }
                if (val == "") continue;
                inputState = true;
                str = str.replace("txt_", "");
                var arr = str.split("_");
                var classEvalItemID = parseInt(arr[0]);
                classID = parseInt(arr[1]);
                var param = parseFloat(val);
                var score = inputList[j].getAttribute("Score") == null ? -1 : parseFloat(inputList[j].getAttribute("Score"));
                var operator = inputList[j].getAttribute("Math") == "" ? 0 : parseFloat(inputList[j].getAttribute("Math"));
                var type = parseInt(inputList[j].getAttribute("MoralityType"));
                if (type == 2 && param < 0) {
                    stuList = [];
                    //showNoticMsg(inputList[j].id, "计次题，请填写大于或等于0的次数", 0, -40);
                    showMassgeBox("提示","计次题请填写大于或等于0的次数");
                    return;
                }
                else if(type == 1 && score < param) {
                    //showNoticMsg(inputList[j].id, "计分题，请填写小于最大分数的的数字", 0, -40);
                    showMassgeBox("提示","计分题请填写小于最大分数的的数字");
                    return;
                }
                if (isNaN(operator)) operator = "0";
                var item = "{\"ClassEvalItemID\": "+classEvalItemID+", \"ClassID\": "+classID+", \"Param\": "+param+", \"Score\": "+score+", \"Type\": "+type+", \"Operator\": "+operator+"}";
                stuList.push(item);
            }
            /* 填写了该班级的资料 */
            if (inputState) {
                stuJsonList = stuList.join(",");
                stuJsonList = "{\"ClassID\": "+classID+",\"ClsItems\":[" + stuJsonList + "]";
                if (spanRemark != undefined && spanRemark.title != "") stuJsonList += ", \"Remark\": \"" + spanRemark.title + "\"";
                stuJsonList += "}";
                list.push(stuJsonList);
            }
            /* 只填了备注 */
            else {
                if (spanRemark != undefined && spanRemark.title != "") {
                    studentID = parseInt(spanRemark.id.replace("span_",""));
                    stuJsonList = "{\"ClassID\": "+classID+",\"Remark\": \"" + spanRemark.title + "\"}";
                    list.push(stuJsonList);
                }
            }
        }
        
        if (stuJsonList.length > 0) {
            jsonList = list.join(",");
            jsonList = "[" + jsonList + "]";
        }
        var schoolYear = request("SchoolYear");
        if (schoolYear == "") schoolYear = -1;
        else schoolYear = parseInt(schoolYear);
        
        var term = request("Term");
        if (term == "") term = -1;
        else term = parseInt(term);
        
        var evalDate = new Date();
        var date = request("evalDate");
        var dateVar = date.split("-");
        if(dateVar.length != 3) 
            evalDate = new Date();
        else 
            evalDate = new Date(dateVar[0], dateVar[1], dateVar[2]);

//        var year = request("Year");
//        if (year == "") year = -1;
//        else year = parseInt(year);
//        
//        var month = request("Month");
//        if (month == "") month = -1;
//        else month = parseInt(month);
        
//        var clsID = request("ClsID");
//        if (clsID == "") clsID = -1;
//        else clsID = parseInt(clsID);
        //debugger;
        PageMethods.SaveClassEvalScore(jsonList, schoolYear, term, date, Number(request("evalID")), Number(request("attribute")), 
            function(succ){ if (succ) showMassgeBox("保存成功","保存成功"); else showMassgeBox("保存失败","保存失败, 请检查数据是否录入或者录入的数据是否符合规则."); }, 
            function(err){return;}
        );
    }

    // 导出Excel
    function getExcel() {
        var cells = GetTableInfo(document.getElementById("tblHead"));
        if(cells!=null)
        {
            PageMethods.GetExcel(cells,
                function(result){
                    document.getElementById('downloadf').src = "";
                    document.getElementById('downloadf').src = "../../../PostFileBytes.aspx";
                },
                function(err){alert(err._message);}
            );
        }
    }
</script>
</html>

<script language="javascript">
        var h=0;
        var w=0;
        if(document.body.scrollHeight>document.getElementById("tblHead").clientHeight) {
            h = document.getElementById("tblHead").clientHeight;
        } else {
            h = document.body.scrollHeight-100;
        }
        if(document.body.scrollWidth>document.getElementById("tblHead").clientWidth) {
            w = document.getElementById("tblHead").clientWidth+20;
        } else {
            w = document.body.scrollWidth-30;
        }
        if(h>0 && w>0) {
            document.getElementById("divTable").style.height=h+"px";
            document.getElementById("divTable").style.width=w+"px";
        }
</script>